<template>
<view class="beforeSearch">
  <view class="searchHistoryContainer">
    <view class="historyTitle">
      <text class="left" v-if="searchHistory.length || (!searchHistory.length && !hideTitle)">搜索历史</text>
      <view class="right" v-if="searchHistory&&searchHistory.length">
        <text @click="handleDeleteClick"  class="icon icon-delete">
        </text>
      </view>
    </view>
    <view class="historyButtons">
      <view class="leftArea">
        <view v-for="(item,index) in historyTempList" @click="handleTagClick(item.text)" :key="index" class="history-tag u-line-1">{{item.text}}</view>
        <view class="history-tag" v-if="searchHistory > 7" @click="handleUnfold">
          <u-icon v-show="isFold" name="arrow-down" color="#999999"></u-icon>
          <u-icon v-show="!isFold" name="arrow-up"  color="#999999"></u-icon>
        </view>
      </view>
    </view>
  </view>
</view>
</template>

<script>
export default {
  props: {
    localKey: {
      type: String,
      default: '',
    },
    hideTitle: Boolean,
  },
  data() {
    return {
      searchHistory: [],
      isFold: true,
    }
  },
  computed: {
    historyTempList() {
      if(this.isFold) {
        return this.searchHistory.slice(0, 7);
      } else {
        return this.searchHistory;
      }
    }
  },
  created() {
    this.getHistory();
  },
  methods: {
    getHistory() {
      this.searchHistory = uni.getStorageSync(this.localKey) && JSON.parse(uni.getStorageSync(this.localKey)) || [];
    },
    saveHistory(keyword) {
      let index = this.searchHistory.findIndex(item => item.text == keyword);
      if (index > -1) {
        this.searchHistory.splice(index, 1);
      }
      this.searchHistory.unshift({ text: keyword });

      if(this.searchHistory.length > 20) {
        this.searchHistory = this.searchHistory.slice(0, 20);
      }

      uni.setStorageSync(this.localKey, JSON.stringify(this.searchHistory));
    },
    // 对搜索历史去重
    unique(arr) {
      let newArr = [];
      arr.forEach((i, v) => {
        let flag = true;
        newArr.forEach(item => {
          if (i.text == item.text)
            flag = false;
        })
        flag ? newArr.push(i) : newArr;
      })
      return newArr
    },
    handleDeleteClick() {
      this.searchHistory = [];
      uni.removeStorageSync(this.localKey);
    },
    handleTagClick(keyword) {
      this.saveHistory(keyword);
      this.$emit('tagClick', keyword);
    },
    handleUnfold() {
      this.isFold = !this.isFold;
    },
  }
}
</script>

<style lang="scss" scoped>
.beforeSearch {
  .searchHistoryContainer {
    margin-top: 30rpx;
    padding: 0 32rpx;
    .historyTitle {
      display: flex;
      justify-content: space-between;
      align-items: center;
      height: 56rpx;
      margin-bottom: 29rpx;
      .left {
        width: 128rpx;
        height: 56rpx;
        font-size: 32rpx;
        font-family: PingFang-SC-Bold, PingFang-SC;
        font-weight: bold;
        color: #222222;
        line-height: 54rpx;
      }

      .right {
        display: flex;
        justify-content: center;
        align-items: center;
        .deleteButton {
          margin-right: 10rpx;
        }
        .deleteIcon {
          width: 40rpx;
          height: 40rpx;
        }
      }
    }

    .historyButtons {
      position: relative;
      display: flex;
      justify-content: space-between;
      .leftArea {
        display: inline-flex;
        flex-wrap: wrap;
        align-content: flex-start;
      }
      .history-tag {
        display: inline-block;
        align-items: center;
        justify-content: center;
        background: #F1F1F1;
        padding: 0rpx 16rpx;
        max-width: 616rpx;
        height: 48rpx;
        line-height: 48rpx;
        margin: 12rpx 0rpx;
        margin-right: 32rpx;
        font-size: 28rpx;
        color: #333333;
        border: 0;
        border-radius: 4rpx;
      }
        
      .moreHistoryup,
      .moreHistorydown {
        position: relative;
        width: 64rpx;
      }
      .moreHistoryup::after {
        content: " ";
        display: block;
        width: 18rpx;
        height: 18rpx;
        border-top: 1px solid #bac3d5;
        border-left: 1px solid #bac3d5;
        position: absolute;
        bottom: 8rpx;
        right: 9rpx;
        transform: rotate(45deg);
      }
      .moreHistorydown::after {
        content: " ";
        display: block;
        width: 18rpx;
        height: 18rpx;
        border-top: 1px solid #bac3d5;
        border-left: 1px solid #bac3d5;
        position: absolute;
        bottom: 8rpx;
        right: 9rpx;
        transform: rotate(-135deg);
      }
    }

    .icon-delete {
      width: 36rpx;
      height: 36rpx;
      background-image: url($img-base + 'report/delete.png');
    }
  }
}
</style>